home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / mathematica / tex-mma.tar_z / tex-mma / math.el < prev    next >
Lisp/Scheme  |  1991-05-14  |  43KB  |  1,100 lines

  1. ;; math.el, a mode package for Mathematica.  
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;; Copyright (c) 1990, 1991 Hewlett-Packard Company, all rights reserved.
  4. ;; 
  5. ;;                             LEGAL NOTICE
  6. ;; 
  7. ;; This math-mode package is experimental and HP shall have no obligation to
  8. ;; maintain or support it.  HP makes no express or implied warranty of any
  9. ;; kind with respect to this software, and HP shall not be liable for any
  10. ;; direct, indirect, special, incidental or consequential damages (whether
  11. ;; based on contract, tort or any other legal theory) arising in any way from
  12. ;; use of the software.
  13. ;; 
  14. ;; Everyone is granted permission to copy, modify and redistribute this
  15. ;; math-mode package, provided:
  16. ;;  1.  All copies contain this copyright notice.
  17. ;;  2.  All modified copies shall carry a prominant notice stating who
  18. ;;      made the last modification and the date of such modification.
  19. ;;  3.  No charge is made for this software or works derived from it.  
  20. ;;      This clause shall not be construed as constraining other software
  21. ;;      distributed on the same medium as this software, nor is a
  22. ;;      distribution fee considered a charge.
  23. ;;
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;; Portions of this package were adapted from GNU Emacs.
  26. ;;
  27. ;; Copyright (C) 1985, 1986, 1987, 1988 Free Software Foundation, Inc. 
  28. ;; 
  29. ;; GNU Emacs is distributed in the hope that it will be useful,
  30. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  31. ;; accepts responsibility to anyone for the consequences of using it
  32. ;; or for whether it serves any particular purpose or works at all,
  33. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  34. ;; License for full details.
  35. ;;
  36. ;; Everyone is granted permission to copy, modify and redistribute
  37. ;; GNU Emacs, but only under the conditions described in the
  38. ;; GNU Emacs General Public License.   A copy of this license is
  39. ;; supposed to have been given to you along with GNU Emacs so you
  40. ;; can know your rights and responsibilities.  It should be in a
  41. ;; file named COPYING.  Among other things, the copyright notice
  42. ;; and this notice must be preserved on all copies.
  43. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  44. ;;    Author: David Jacobson, jacobson@hplabs.hp.com
  45. ;;      Experimental version of January 30, 1991
  46. ;;      Assumes GNU Emacs version 18.54 or later
  47. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  48. ;; 2/17/1991 Dan Dill dan@chem.bu.edu
  49. ;;   Add math-send-filter-active and math-send-filter-status, 
  50. ;;   for use with tex-mma
  51. ;;
  52. ;; 5/1/1991 Dan Dill dan@chem.bu.edu
  53. ;;   Add math-remote-host and math-remote-shell and modified 
  54. ;;   start-buffer-process, to run Mathematica remotely
  55. ;;
  56. ;; 5/8/1991 David Jacobson jacobson@hplabs.hp.com
  57. ;;   Add math-timeout and improve documentation, add checking
  58. ;;   for incomplete cells to check-math-syntax
  59. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  60.  
  61. (provide 'math)
  62.  
  63. (require 'shell)
  64.  
  65. (defvar Mathematica-search-path 
  66.   (list nil (getenv "HOME") "/usr/local/math/Init" "/usr/local/math/Packages")
  67.   "*A list of directories in which to look for files.  
  68. Use nil for the current directory.")
  69.  
  70. (defvar math-process-string "/usr/local/bin/math"
  71.   "*A string to pass to the unix exec function to start Mathematica")
  72.  
  73. (defvar math-process-buffer 
  74.   "*math*"
  75.   "The buffer normally running Mathematica.  Certain commands
  76. (e.g. math-complete-symbol) will go to this buffer to find a Mathematica 
  77. process.  This can be locally set with set-math-process-buffer.")
  78.  
  79. (defconst math-header-re (concat "^" (regexp-quote "by S. Wolfram, D. Grayson, R. Maeder, H. Cejtin,"))
  80.   "A regexp that will match somewhere in the Mathematica preamble")
  81.                            
  82. (defvar math-remote-host nil
  83.   "*If non-nil, use as remote host name with `math-remote-shell' to run
  84. Mathematica remotely.")
  85.  
  86. (defvar math-timeout nil 
  87. "*If non-nil, use as a timeout between lines of input.  Nil is
  88. recommended, unless you are having trouble with the system hanging,
  89. particularly on syntax errors.  Nil causes input to be waited for
  90. using accept-process-output.  This means the next line of output will
  91. be send when math-mode receives the indentation prompt from
  92. Mathematica.  If non-nil math-mode will wait only math-timeout seconds
  93. between sending lines of input. Usually this needs to be set when
  94. using a remote host.  1 is recommended in this case. (See
  95. math-remote-host and math-remote-shell.)")
  96.  
  97. (defvar math-remote-shell 
  98.   (cond ((file-exists-p "/usr/ucb/rsh")    "/usr/ucb/rsh")
  99.     ((file-exists-p "/usr/bin/remsh") "/usr/bin/remsh"))
  100.   "*String used with `math-remote-host' to run Mathematica remotely.")
  101.  
  102. (defconst math-valid-cell-ending-re 
  103.   ".*\\([])}\"A-Za-z0-9!_'$%#]\\|\\+\\+\\|--\\|=[ \t]*\\.\\|[^/];\\|\\b[0-9]+\\.\\|[^&]&\\)[ \t]*$"
  104.   "An re that matches lines that can validly end complete cells.
  105. This is not perfect.")
  106. ;;; The "[^/];" is because "/;" means a condition follows, so it cannot 
  107. ;;; be the end of a cell.  The "=[ \t]*\\." and "\\b[0-9]+\\." allow periods 
  108. ;;; only in "=." and at the end of mumbers, disallowing a 
  109. ;;; variable followed by a period (Dot[]).  "[^&]&" allows "&" but not "&&".  
  110. ;;; It will mess up if "&" appears
  111. ;;; as the first character on a line by itself, but then the end of the
  112. ;;; previous line probably made a valid prefix, which is an error anyway.
  113.  
  114. (defvar math-send-filter-status 'normal
  115.   "Status of last input to Mathematica:
  116.   `normal' means no problems detected;
  117.   `premature-output' means part of cell not sent due to unexpected output;
  118.   `blank-line-added' means line inserted to separate input from output;
  119.   `incomplete-cell' means an incomplete cell was detected;
  120.   `syntax-error' means a syntax error was detected.")
  121.  
  122. (defvar math-send-filter-active nil
  123.   "Status of Mathematica process filter: t if enabled, else nil.")
  124.  
  125. (defvar math-mode-map nil) 
  126.  
  127. ;;; deep-copy-keymap was written by Daniel LaLiberte.
  128. ;;; Some GNU Emacs systems already have this installed.  If so
  129. ;;; comment out this function definition.
  130.  
  131. (defun deep-copy-keymap (keymap)
  132.   "Return a deep copy of KEYMAP.  That is, all levels are copied,
  133. not just the top level."
  134.   (if (not (keymapp keymap))
  135.       keymap
  136.     (cond
  137.  
  138.      ((listp keymap)
  139.       (let ((new-keymap (copy-alist keymap)))
  140.     (setq keymap (cdr new-keymap))
  141.     (while keymap
  142.       (let ((binding (car keymap)))
  143.         (if (keymapp (cdr binding))
  144.         (setcdr binding (deep-copy-keymap (cdr binding))))
  145.         )
  146.       (setq keymap (cdr keymap))
  147.       )
  148.     new-keymap
  149.     ))
  150.  
  151.       ((vectorp keymap)
  152.        (let ((i 0)
  153.          (n (length keymap))
  154.          (new-keymap (copy-sequence keymap)))
  155.      (while (< i n)
  156.        (if (keymapp (aref keymap i))
  157.            (aset new-keymap i (deep-copy-keymap (aref keymap i))))
  158.        (setq i (1+ i)))
  159.      new-keymap
  160.      )))))
  161.  
  162.  
  163. (if math-mode-map
  164.     nil
  165.   (setq math-mode-map (deep-copy-keymap shell-mode-map))
  166.   (define-key math-mode-map "\C-m" 'newline) 
  167.                     ; The shell-mode-mode
  168.                     ; sets this to shell-send-input.
  169.                     ; We change it to 'newline.  We
  170.                     ; ought to undefine it, so that
  171.                     ; Emacs will find the 'newline
  172.                     ; in the global keymap, but there
  173.                     ; is no easy way to do this.
  174.   (define-key math-mode-map "\e\C-m" 'math-send-input)
  175.   ;; \C-c\C-c is set to interrupt-shell-subjob in the shell-mode-mode
  176.   ;; The name is deceptive; it sends a SIGINT signal (control C) to 
  177.   ;; whatever process is running in the current buffer
  178.   (define-key math-mode-map "\C-c9" 'kill-9-process)
  179.   (define-key math-mode-map "\C-c\C-h"   'math-help); buffer specific C-h
  180.   (define-key math-mode-map "\C-he" 'math-help) ; e-xpression in help menu
  181.   (define-key math-mode-map "\C-hE" 'math-extra-help) ; E-xpression in help menu
  182.   (define-key math-mode-map "\C-c\C-f" 'math-edit-function)
  183.   (define-key math-mode-map "\e\t"   'math-complete-symbol)
  184.   (define-key math-mode-map "\C-c\C-y" 'math-copy-cell)
  185.   (define-key math-mode-map "\C-c\C-e" 'find-math-error)
  186.   (define-key math-mode-map "\C-c\C-r" 'math-isearch-backward))
  187.  
  188. (defvar math-mode-syntax-table nil
  189.   "Syntax table used while in math mode.")
  190.  
  191. (if math-mode-syntax-table
  192.     ()
  193.   (setq math-mode-syntax-table (make-syntax-table))
  194.   (modify-syntax-entry ?% "." math-mode-syntax-table)
  195.   (modify-syntax-entry ?& "." math-mode-syntax-table)
  196.   (modify-syntax-entry ?* ". 23" math-mode-syntax-table) ;allow for (* comment *)
  197.   (modify-syntax-entry ?+ "." math-mode-syntax-table)
  198.   (modify-syntax-entry ?- "." math-mode-syntax-table)
  199.   (modify-syntax-entry ?/ "." math-mode-syntax-table)
  200.   (modify-syntax-entry ?< "." math-mode-syntax-table)
  201.   (modify-syntax-entry ?= "." math-mode-syntax-table)
  202.   (modify-syntax-entry ?> "." math-mode-syntax-table)
  203.   (modify-syntax-entry ?_ "." math-mode-syntax-table)
  204.   (modify-syntax-entry ?\| "." math-mode-syntax-table)
  205.   (modify-syntax-entry ?\` "_" math-mode-syntax-table) ; Mathematica context symbol
  206.   (modify-syntax-entry ?\( "()1" math-mode-syntax-table) ;allow for (* comment *)
  207.   (modify-syntax-entry ?\) ")(4" math-mode-syntax-table)) ;allow for (* comment *)
  208.  
  209. ;;; math-send-input sends a chunk of text to Mathematica.  It
  210. ;;; interacts tightly with math-send-filter using the buffer-specific
  211. ;;; variable math-send-state and synchronizes though sending output
  212. ;;; and the accept-process-output (but see below) command.  
  213.  
  214. ;;; The Emacs documentation
  215. ;;; claims that one cannot be sure how much output will be delivered
  216. ;;; in a chunk to the output filter.  However we depend on chunks of
  217. ;;; up to one line after which Mathematica immediately does a read,
  218. ;;; arriving in a single chunk.  A slightly more robust technique than
  219. ;;; doing string-match on the input string would be to put the input 
  220. ;;; into the buffer and match there.  
  221.  
  222. ;;; If the Mathematica process does not send a prompt at all, the
  223. ;;; accept-process-output hangs and the only solution is to kill the
  224. ;;; mathexe process or Emacs.  This can happen if you use an Input[""]
  225. ;;; (a rather perverse thing to do).  An alternative is to replace
  226. ;;; (accept-process-output process) with (sleep-for 10).  It appears that
  227. ;;; arrival of any output causes Emacs to pop immediately out of a
  228. ;;; sleep-for.  But this could mess up if you have more than one active
  229. ;;; process running at a time or if you strike a key.  
  230. ;;; Of course, we could use a while loop and have the filter set 
  231. ;;; math-send-state to another value when it has actually gotten 
  232. ;;; something.  I tried this but ran into unknown trouble and have 
  233. ;;; not followed up on it.
  234.  
  235. ;;; The variable math-send-state has the following interpretatons:
  236. ;;; non-last-line             We are in the middle of sending a
  237. ;;;                           multi-line input.  Watch for
  238. ;;;                           errors and output other than indent
  239. ;;;                           cookies.
  240. ;;;
  241. ;;; last-line                 The last line has been sent, still watch
  242. ;;;                           for syntax error messages.  Also insert
  243. ;;;                           a blank like (and warn about it) if the
  244. ;;;                           output contained any non-whitespace
  245. ;;;                           characters before a newline.
  246. ;;;               
  247. ;;; throw-away-prompt         A syntax error has been detected and a 
  248. ;;;                           newline sent to Mathematica to flush its
  249. ;;;                           input buffer.  Normally it will come
  250. ;;;                           back with a new prompt.  If the next
  251. ;;;                           output looks like a prompt, throw it
  252. ;;;                           away and give a syntax error message.
  253. ;;;                           Throw away indent cookies.
  254. ;;;                           Otherwise display the discarded
  255. ;;;                           material in a warning message.
  256. ;;;
  257. ;;; normal                    Just post the output.  Actually I think
  258. ;;;                           everything that sets the mode to normal
  259. ;;;                           also sets the filter to nil, so this is
  260. ;;;                           hardly used.  (But see the exit clause of 
  261. ;;;                           the unwind-protect.)
  262.  
  263.  
  264. (defun math-send-input ()
  265.   "Send input to Mathematica.
  266. At end of buffer, sends last \"cell\" to Mathematica.  When not at end, 
  267. copies current \"cell\" to the end of the buffer and sends it.  Also
  268. sends input for interrupt and Input[].  Warning: A multi-line input
  269. to Input[\"\"] will cause deadlock."
  270.   (interactive "*")
  271.   (let ((process (or (get-buffer-process (current-buffer))
  272.              (error "Current buffer has no process")))
  273.     bpt2
  274.     ept2
  275.     begpkt
  276.     endpkt
  277.     copy
  278.     )
  279.     ;; Find beginning of "cell"
  280.     (let* ((cellinfo (math-identify-cell (point) (process-mark process)))
  281.        (bpt (car cellinfo))
  282.        (ept (nth 1 cellinfo))
  283.        )
  284.       (check-math-syntax bpt ept)
  285.       (goto-char ept)
  286.       ;; Move to line beyond cell, adding newline if necessary.
  287.       (forward-line 1)
  288.       (if (or (not (bolp)) 
  289.           (= (point) bpt)) ; make null cells contain a newline
  290.       (newline))
  291.       (setq copy (buffer-substring bpt (point)))
  292.       ;; If we are \"near\" the end of the buffer, we don't copy the data down
  293.       ;; there, but we kill excess white space.  Otherwise, we go there and 
  294.       ;; copy the data.
  295.       (if (looking-at "\\s *\\'")
  296.       (progn
  297.         (replace-match "")
  298.         (setq bpt2 bpt)
  299.         (setq ept2 (point))
  300.         (setq math-last-input-end (point)))
  301.     (goto-char (point-max))
  302.     (forward-line 0)
  303.     (if (or (eolp) (looking-at "^In\\[[0-9]+\\]:=\\s *$"))
  304.         (end-of-line)
  305.       (end-of-line)
  306.       (newline))
  307.     (setq bpt2 (point))
  308.     (insert copy)
  309.     (setq ept2 (point))
  310.     (setq math-last-input-end (point)))
  311.       (goto-char bpt2)
  312.       ;; indentstring is a global variable
  313.       ;; indentstring is a the string Mathematica would have indented
  314.       ;; the user's start point by if we were talking to it directly.
  315.       (setq math-indent-string "")
  316.       (setq math-send-filter-status 'normal) ; For single line input without filter
  317.                                              ; ..
  318.       (set-process-filter process 'math-send-filter)
  319.       ;; math-send-state is a global variable
  320.       (setq math-send-state 'non-last-line)
  321.       (setq begpkt bpt2) ; point
  322.       (message "*")
  323.       (unwind-protect
  324.     (while (eq math-send-state 'non-last-line)
  325.       (goto-char begpkt)
  326.       (forward-line 1)
  327.       (setq endpkt (point))
  328.       (if (= endpkt ept2) (setq math-send-state 'last-line))
  329.       (metered-process-send-string 
  330.        process (buffer-substring begpkt endpkt))
  331.       (if (eq math-send-state 'non-last-line)
  332.           (if math-timeout
  333.           (sleep-for math-timeout)
  334.         (accept-process-output process))
  335.         )
  336.       (cond ((eq math-send-state 'premature-output)
  337.          (set-process-filter process nil)
  338.          (setq math-send-filter-active nil) 
  339.          (setq math-send-filter-status 'premature-output)
  340.          (setq math-send-state 'normal) ; for unwind protect
  341.          (error  "Unexpected output; part of cell discarded")))
  342.       (setq begpkt endpkt) ; advance to next line
  343.       ) ; end while
  344.     ;; unwind-protect tail
  345.     (if (memq math-send-state '(last-line normal))
  346.         ()
  347.       (progn
  348.         (set-process-filter process nil)
  349.         (setq math-send-filter-active nil)))))))
  350.  
  351.  
  352.  
  353. (defun math-send-filter (proc string)
  354.   (let ((cbuf (current-buffer))
  355.     (save-match-data (match-data)))
  356.     (unwind-protect
  357.     (progn
  358.       (set-buffer (process-buffer proc))
  359.       (cond 
  360.        ;; cond branch: a <retype-line error>
  361.        ((and
  362.          (memq math-send-state '(non-last-line last-line))
  363.          (string-match "\\`\\([ \t]*\\)\\^ <retype line>" string))
  364.         (let ((tpt (point))
  365.           error-column
  366.           indent-column
  367.           (tail-string (substring string (match-end 0))))
  368.           (goto-char tpt)
  369.           (insert (substring string 0 (match-end 1)))
  370.           (setq error-column (current-column))
  371.           (delete-region tpt (point))
  372.           (insert math-indent-string)
  373.           (setq indent-column (current-column))
  374.           (delete-region tpt (point))
  375.           (indent-to-column (- error-column indent-column))
  376.           (insert "^--error\n")
  377.           (backward-char 9)
  378.           (previous-line 1)
  379.           ;; Display any unexpected output.  I don't know how to 
  380.           ;; test this code.
  381.           (if (string-match "\\S " tail-string)
  382.           (save-excursion
  383.             (goto-char (point-max))
  384.             (insert tail-string))))  ; end of let
  385.         (setq math-send-state 'throw-away-prompt)
  386.         (message "Syntax error") ; live dangerously here, but sometimes we 
  387.                     ; don't get a prompt back from 
  388.                     ; Mathematica
  389.         (process-send-string proc "\n"))
  390.        ;; cond branch: snarf up indent strings
  391.        ;; Whether or not this branch is taken when math-send-state
  392.        ;; is throw-away-prompt depends on the OS.  On some systems
  393.        ;; the indent cookie comes out with the "^ <retype-line>" 
  394.        ;; and the indent cookie is inserted by the tail-string
  395.        ;; procesing above.  On others it comes out separately and is 
  396.        ;; handled here.
  397.        ((and
  398.          (memq math-send-state '(non-last-line throw-away-prompt))
  399.          (string-match "\\`[ \t]+\\'" string))
  400.         (setq math-indent-string string))
  401.        ;; cond branch: unexpected output
  402.        ((eq math-send-state 'non-last-line)
  403.         (insert 
  404. "-------- Unexpected output appeared here; rest of cell not sent --------\n"
  405. )
  406.         (goto-char (point-max))
  407.         (insert string)
  408.         (set-marker (process-mark proc) (point))
  409.         (setq math-send-state 'premature-output)
  410.         (message ""))
  411.        ;; cond branch: throw away unwanted prompt
  412.        ((eq math-send-state 'throw-away-prompt)
  413.          (setq math-send-state 'normal)
  414.          (set-process-filter proc nil)
  415.          (setq math-send-filter-status 'syntax-error)
  416.          (setq math-send-filter-active nil)
  417.          (if (string-match "\\`In\\[[0-9]+\\]:= \\'" string)
  418.          (message "Syntax error")
  419.            (message "Syntax error, discarding prompt(?): %s" string)))
  420.        ;; cond branch: last line has been sent, make sure a blank line
  421.        ;; follows the In... stuff.  See further comments.
  422.        ((eq math-send-state 'last-line)
  423.         (goto-char (point-max))
  424.         (cond ((string-match "\\`\\s *\n" string); blank with newline 
  425.            (message ""))                ; clear "*" in message area
  426.           ((string-match "\\`\\s *\\'" string) ; all blank no newline
  427.                     ; probably an indent cookie
  428.                     ; give help message
  429.            (setq math-send-filter-status 'incomplete-cell)
  430.            (message 
  431.             "Incomplete cell?  (Clear with RET ESC RET)"))
  432.           ((string-match "\\`In\\[[0-9]+\\]:=" string)
  433.            (setq math-send-filter-status 'normal)
  434.            (message "")) ; clear "*" in message area
  435.           (t
  436.            ;; non-blank, but not an In[] prompt.  Probably either
  437.            ;; output of a Mathematica Print[...] or an error message.
  438.            ;; Add a blank line to separate cells.
  439.            (newline)
  440.            (setq math-send-filter-status 'blank-line-added)
  441.            (message "newline inserted by Emacs' math-mode")))
  442.         (insert string)
  443.         (set-marker (process-mark proc) (point))
  444.         (setq math-send-state 'normal)
  445.         (set-process-filter proc nil)
  446.         (setq math-send-filter-active nil))
  447.        ;; cond branch.  I think this should never be taken.  It should be
  448.        ;; the case that everything that sets math-send-state to normal
  449.        ;; also sets the process filter to nil.
  450.        (t
  451.         (setq math-send-state 'normal)
  452.         (set-process-filter proc nil)
  453.         (setq math-send-filter-status 'normal)
  454.         (setq math-send-filter-active nil)
  455.         (goto-char (point-max))
  456.         (insert string)
  457.         (set-marker (process-mark proc) (point)))
  458.        ))  ; finishes up cond and progn
  459.       ;; safely exit the filter
  460.       (set-buffer cbuf)
  461.       (store-match-data save-match-data))))
  462.  
  463. (defun math-mode ()
  464.   "Major mode for interacting with Mathematica and editing .m files.
  465.  
  466. \\[math] starts Mathematica.  (See below for starting Mathematica on a 
  467. remote host.)
  468.  
  469. \\[math-send-input] tries to identify stuff following last \"In[...]:=
  470. \" or blank line or the last output and sends it.  To clear out
  471. Mathmatica after an error occurs, move point two lines below last
  472. printing character and type \\[math-send-input].  Warning: do not
  473. use Input[\"\"], and type in a mult-line reply; deadlock results.
  474.  
  475. \\[math-help] gives help on a Mathematica symbol.  
  476. \\[math-extra-help] or C-u \\[math-help] gives more verbose help.
  477.  
  478. \\[math-complete-symbol] will complete the symbol near point.
  479.  
  480. \\[math-copy-cell] will copy a previous cell to the end of the buffer.
  481. See its description for details.
  482.  
  483. \\[math-isearch-backward] does a backward regexp i-search, 
  484. initialized to find In[...].
  485.  
  486. \\[find-math-error] when typed after <<filename has returned a
  487. syntax error will goto the error.  (Depends on Mathematica-search-path.)
  488.  
  489. \\[goto-matherr-line] will go to the specificied line, with lines
  490. counted as Mathematica does.  For use in .m files.
  491.  
  492. \\[interrupt-shell-subjob] interrupts Mathematica.
  493. \\[kill-9-process] kills (-9) the Mathematica process.
  494.  
  495. \\[start-math] starts a Mathematica process in the current buffer.
  496.  
  497. Most entries from the Emacs' shell mode are available as well.
  498.  
  499. If you are not in a buffer running Mathematica, \\[math-help], \\[math-extra-help], 
  500. \\[math-complete-symbol], and \\[math-copy-cell] use or copy to the 
  501. buffer *math*.  \\[math-help], \\[math-extra-help], and \\[math-complete-symbol]
  502. all send input to Mathematica: chaos may ensue if you do this while Mathmatica
  503. is busy with other work---no check is made.  You can change the buffer/process
  504. these commands use with \\[set-math-process-buffer].
  505.  
  506. Entry to this mode calls the value of math-mode-hook with no args,
  507. if that value is non-nil.
  508.  
  509. If variable math-remote-host is non-nil, \\[math] will start
  510. Mathematica on host math-remote-host using command in
  511. math-remote-shell, which defaults to \"/usr/ucb/rsh\", or 
  512. \"/usr/bin/remsh\" if the first is not available.  If you have trouble
  513. with math-mode hanging multi-line input, see the help on the variable
  514. math-timeout.  Errors are often not dealt with correctly when a remote
  515. host is used; sorry."
  516.  
  517.  
  518.   (interactive)
  519.   (kill-all-local-variables)
  520.   (setq major-mode 'math-mode)
  521.   (setq mode-name "Mathematica")
  522.   (setq mode-line-process '(": %s"))
  523.   (use-local-map math-mode-map)
  524.   (set-syntax-table math-mode-syntax-table)
  525.   (make-local-variable 'parse-sexp-ignore-comment)
  526.   (setq parse-sexp-ignore-comment t)
  527.   (make-local-variable 'math-indent-string)
  528.   (make-local-variable 'math-send-state)
  529.   (setq math-send-state 'normal)
  530.   (make-local-variable 'doing-math-complete-symbol)
  531.   (setq doing-math-complete-symbol nil)
  532.   ; Position of end of last input to Mathematica
  533.   (make-local-variable 'math-last-input-end)
  534.  
  535.   (run-hooks 'math-mode-hook))
  536.  
  537.  
  538. (defun math ()
  539.   "Run Mathematica, input and output via buffer *math*."
  540.   (interactive)
  541.   (pop-to-buffer (start-buffer-process
  542.           "*math*" "math" math-process-string
  543.           nil))
  544.   (math-mode)
  545.   ;; We don't make this one local.  That way if the
  546.   ;; user changes the name of the buffer, say by writing
  547.   ;; it to a file, math-process-buffer still points
  548.   ;; to the right place.
  549.   (setq math-process-buffer (current-buffer)))
  550.  
  551. (defun start-math ()
  552.   "Starts a Mathematica process in the current buffer."
  553.   (interactive "*")
  554.   (start-buffer-process (current-buffer) "math" math-process-string
  555.             nil)
  556.   (math-mode)
  557.   (make-local-variable 'math-process-buffer)
  558.   (setq math-process-buffer (current-buffer)))
  559.  
  560.  
  561. (defun math-complete-symbol ()
  562.   "Complete the symbol preceeding point."
  563.   (interactive "*")
  564.   (let ((process (get-buffer-process math-process-buffer))
  565.     sent-successfully)
  566.     (if    (not (and process (memq (process-status process) '(run stop))))
  567.     (error "No math process running in buffer %s" math-process-buffer))
  568.     (setq math-completion-symbol (math-symbol-around-point))
  569.     (unwind-protect
  570.     (let ((cbuf (current-buffer)))
  571.       (set-buffer (get-buffer-create " Mathwork"))
  572.       (erase-buffer)
  573.       (set-buffer cbuf)
  574.       (setq doing-math-complete-symbol t)
  575.       (set-process-filter process 'math-help-filter)
  576.       (process-send-string process (concat 
  577. "Scan[Print,Names[\"" math-completion-symbol "**\"]];Out[--$Line];\n"))
  578.       (setq sent-successfully t))
  579.       (if (not sent-successfully)
  580.       (progn
  581.         (setq doing-math-complete-symbol nil)
  582.         (set-process-filter process nil))))))
  583.        
  584.               
  585. (defun math-symbol-around-point ()
  586.  "Return the symbol around the point as a string."
  587.  (save-excursion
  588.    (let (beg)
  589.      (if (not (eobp)) (forward-char 1))
  590.      (if (not (re-search-backward "\\w\\|\\s_" nil t))
  591.      ""
  592.        (forward-char 1)
  593.        (backward-sexp)
  594.        (setq beg (point))
  595.        (forward-sexp)
  596.        (buffer-substring beg (point))))))
  597.  
  598. (defun math-extra-help () 
  599.   "Like math-help with a prefix arg"
  600.   (interactive)
  601.   (let ((current-prefix-arg (list 1))
  602.     (prefix-arg (list 1)))          ; I'm hacking.  
  603.                     ; current-prefix-arg makes M-X ... work
  604.                                         ; prefix-arg makes it work when bound to a key
  605.                     ; I'm sure RMS had something else in mind.
  606.     (call-interactively 'math-help)))
  607.  
  608. (defun math-help (symbol arg)
  609.   "Display what Mathematica knows about SYMBOL.  
  610. With prefix arg (2nd arg when called from a program) it gives more info."
  611.   (interactive  ; read a word, using the word around point as the default
  612.    (let ((enable-recursive-minibuffers t)
  613.      (try-word (math-symbol-around-point))
  614.      val)
  615.      (if (string-equal try-word "")
  616.      (setq val (read-string "Mathematica symbol: "))
  617.        (setq val (read-string (format "Mathematica symbol (default %s): "
  618.                       try-word)))
  619.        (if (string-equal val "")
  620.        (setq val try-word)))
  621.      (if (string-equal val "")
  622.      (error "No symbol read"))
  623.      (list val current-prefix-arg)))
  624.   (let ((process (get-buffer-process math-process-buffer))
  625.     sent-successfully)
  626.     (if    (not (and process (memq (process-status process) '(run stop))))
  627.       (error "No math process running in buffer %s" math-process-buffer))
  628.     (unwind-protect
  629.     (progn
  630.       (with-output-to-temp-buffer "*Help*"
  631.         (print-help-return-message))
  632.       (set-process-filter process 'math-help-filter)
  633.       (process-send-string process (concat (if arg "??" "?") symbol "\n"))
  634.       (setq sent-successfully t))
  635.       (if (not sent-successfully) (set-process-filter process nil)))))
  636.  
  637.  
  638. (defun math-edit-function (symbol arg)
  639.   "Display all of SYMBOL's definitions in InputForm"
  640.   (interactive  ; read a word, using the word around point as the default
  641.    (let ((enable-recursive-minibuffers t)
  642.      (try-word (math-symbol-around-point))
  643.      val)
  644.      (if (string-equal try-word "")
  645.      (setq val (read-string "Mathematica symbol: "))
  646.        (setq val (read-string (format "Mathematica symbol (default %s): "
  647.                       try-word)))
  648.        (if (string-equal val "")
  649.        (setq val try-word)))
  650.      (if (string-equal val "")
  651.      (error "No symbol read"))
  652.      (list val current-prefix-arg)))
  653.   (let ((sent-successfully)
  654.     (process (get-buffer-process math-process-buffer))
  655.     (cbuf (current-buffer)))
  656.     (unwind-protect
  657.     (progn
  658.       (set-buffer (get-buffer-create " Mathwork"))
  659.       (erase-buffer)
  660.       (set-buffer cbuf)
  661.       (set-process-filter process 'math-edit-function-filter)
  662.       (process-send-string process 
  663.                    (concat "Print[HoldForm[Clear[" symbol 
  664.                        "]]];Definition[" symbol 
  665.                        "]//InputForm\nPrint[\"asdfasdfasdfasdf\"];Out[$Line -= 2];\n"))
  666.       (setq sent-successfully t))
  667.       (if (not sent-successfully)
  668.       (set-process-filter process nil)))))
  669.  
  670. (defun math-edit-function-filter (proc string)
  671.   (let ((cbuf (current-buffer))
  672.     (save-match-data (match-data))
  673.     )
  674.     (unwind-protect
  675.     (progn
  676.       (set-buffer " Mathwork")
  677.       (goto-char (point-max))
  678.       (insert string)
  679.       (forward-line 0)
  680.       (if (and (looking-at "In\\[[0-9]+\\]" )
  681.             (re-search-backward "\\(\\S \\)\\s *In\\[[0-9]+\\]:= asdfasdfasdfasdf" nil t))
  682.           (let ((separator "")) ; separator puts things 
  683.         ; like (*=============*) between the defintions.
  684.         ; but the more I looked at it the more less-cluttered looked
  685.         ; better.  Feel free to change it to whatever you like.
  686.         (set-process-filter proc nil)
  687.         (delete-region (match-end 1) (point-max))
  688.         (goto-char (point-max))
  689.         (if (not (re-search-backward "Out\\[[0-9]+\\]//InputForm=" nil t))
  690.             (error "Math mode internal error"))
  691.         (delete-region (match-beginning 0) (match-end 0))
  692.         (if (looking-at "\\s *\\'") ; blank to eob
  693.             (progn ; extract name of function
  694.               (goto-char (point-min))
  695.               (re-search-forward "Clear\\[\\([^]]+\\)\\]")
  696.               (error "Function %s not defined" 
  697.                  (buffer-substring 
  698.                   (match-beginning 1) (match-end 1)))))
  699.         (if (looking-at "\\s *Attributes\\[.+\\] =")
  700.             (progn 
  701.               (forward-sexp)
  702.               (end-of-line)
  703.               (newline)))
  704.         (goto-char (point-min))
  705.         (insert "(")
  706.         (goto-char (point-max))
  707.         (insert "\n;" separator ")\n")
  708.         (goto-char (point-min))
  709.         (replace-regexp "^\\([ \t]*\n\\)+" (concat ";" separator "\n"))
  710.         (goto-char (point-max))
  711.         (set-buffer cbuf)
  712.         (insert-buffer " Mathwork")
  713.         (forward-line 2)))
  714.       )
  715.       ;; Unwind protect tail
  716.       (set-buffer cbuf)
  717.       (store-match-data save-match-data))))
  718.  
  719.  
  720.  
  721. (defun math-help-filter (proc string)
  722.   (let ((cbuf (current-buffer))
  723.     (save-match-data (match-data))
  724.     (local-doing-math-complete-symbol doing-math-complete-symbol))
  725.     ;; doing-math-complete-symbol is buffer-local and we are going
  726.     ;; to switch buffers.
  727.     (unwind-protect
  728.     (progn
  729.       (if local-doing-math-complete-symbol
  730.           (set-buffer " Mathwork")
  731.         (set-buffer "*Help*"))
  732.       (goto-char (point-max))
  733.       (insert string)
  734.       (beginning-of-line)
  735.       (if (looking-at "^In\\[[0-9]+\\]:=")
  736.           (progn
  737.         (delete-region (point) (point-max))
  738.         (bury-buffer (current-buffer))
  739.         (set-process-filter proc nil)
  740.         (if local-doing-math-complete-symbol
  741.             (progn
  742.               (set-buffer cbuf)
  743.               ;; we are back to the original buffer, so this is ok
  744.               (setq doing-math-complete-symbol nil)
  745.               (insert (get-math-completion math-completion-symbol)))
  746.           (goto-char (point-min))))))
  747.       (set-buffer cbuf)
  748.       (store-match-data save-match-data))))
  749.  
  750. (defun check-math-syntax (pmin pmax)
  751.   "Checks for balanced parens, lack of valid prefix, and valid termination.
  752. Mathematica will misbehave if there exists a prefix of a cell such that
  753. the prefix ends in a newline and forms a valid mathematica expresssion.
  754. This function causes an error if that is the case.  If that is ok it checks 
  755. that the whole expression has balanced parens, comments and quotes.  It is
  756. not perfect at these checks since GNU Emacs does not understand nested
  757. comments.  Also it only checks that the nesting level of all paren constructs
  758. is zero at the end, not that they really match."
  759.   (interactive "r")
  760.   (let ((pt (point))
  761.     possibleerr)
  762.     (save-restriction
  763.       (narrow-to-region pmin pmax)
  764.       (goto-char pmin)
  765.       (while (and (not possibleerr)
  766.           (not (eobp)))
  767.     (end-of-line)
  768.     (let ((parsestate (parse-partial-sexp (point-min) (point))))
  769.       (if (not (looking-at "\\s *\\'")) ; not just all white space to eob
  770.           (progn      ; make sure this could NOT end a valid expression
  771.         (if (and
  772.              (= (nth 0 parsestate) 0) ; zero paren depth
  773.              (not (nth 3 parsestate)) ; not in a string
  774.              (not (nth 4 parsestate)) ; not in a comment
  775.              (progn
  776.                (forward-line 0)
  777.                (looking-at math-valid-cell-ending-re)))
  778.             (progn
  779.               (setq possibleerr 
  780.                 "Possible complete statement before end, submit anyway? ")
  781.               (end-of-line))))
  782.         ;; we are at the end of the statement
  783.         (cond ((nth 3 parsestate)
  784.            (setq possibleerr
  785.              "Apparently unterminated string, submit anyway? "))
  786.           ((nth 4 parsestate)
  787.            (setq possibleerr 
  788.              "Apparently unclosed comment, submit anyway? "))
  789.           ((not (zerop (nth 0 parsestate)))
  790.            (setq possibleerr
  791.              "Apparently mismatched parens, submit anyway? "))
  792.           ((save-excursion
  793.              (forward-line 0)
  794.              (not (looking-at math-valid-cell-ending-re)))
  795.            (setq possibleerr 
  796.              "Possible incomplete cell, submit anyway? "))))
  797.       (if (not possibleerr) (forward-line 1)))))
  798.     (if (and possibleerr (not (y-or-n-p possibleerr)))
  799.     (progn
  800.       (setq math-send-filter-status 'syntax-error) 
  801.       (error "Cancelled"))
  802.       (goto-char pt))))
  803.  
  804.  
  805. (defun start-buffer-process (bufferid procname program &optional startfile &rest switches)
  806.   ;; A munged version of make-shell
  807.   ;; bufferid can be a buffer or the name of a buffer
  808.   (let ((buffer (get-buffer-create bufferid))
  809.     proc proc-args status size)
  810.     (setq proc (get-buffer-process buffer))
  811.     (if proc (setq status (process-status proc)))
  812.     (save-excursion
  813.       (set-buffer buffer)
  814.       ;;    (setq size (buffer-size))
  815.       (if (memq status '(run stop))
  816.       nil
  817.     (if proc (delete-process proc))
  818.     (message "Starting Mathematica...")
  819.     (setq proc-args (list
  820.                          procname
  821.                          buffer
  822.                          (concat exec-directory "env")
  823.                          (format
  824.                           "TERMCAP=emacs:co#%d:tc=unknown:"
  825.                           (screen-width))
  826.                          "TERM=emacs"
  827.                          "EMACS=t"
  828.                          "-"
  829.                          ))
  830.     (if math-remote-host (setq proc-args (append proc-args
  831.                         (list math-remote-shell)
  832.                         (list math-remote-host))))
  833.     (setq proc-args (append proc-args
  834.                 (list
  835.                  (or program explicit-shell-file-name
  836.                      (getenv "ESHELL")
  837.                      (getenv "SHELL")
  838.                      "/bin/sh"))))
  839.     (if switches (setq proc-args (append proc-args switches)))
  840.     (setq proc (apply 'start-process proc-args))
  841.     (cond (startfile
  842.            ;;This is guaranteed to wait long enough
  843.            ;;but has bad results if the shell does not prompt at all
  844.            ;;         (while (= size (buffer-size))
  845.            ;;           (sleep-for 1))
  846.            ;;I hope 1 second is enough!
  847.            (sleep-for 1)
  848.            (goto-char (point-max))
  849.            (insert-file-contents startfile)
  850.            (setq startfile (buffer-substring (point) (point-max)))
  851.            (delete-region (point) (point-max))
  852.            (process-send-string proc startfile)))
  853.     (setq procname (process-name proc)))  ; what in the world is this for?
  854.       (goto-char (point-max))
  855.       (set-marker (process-mark proc) (point))
  856.       (shell-mode))
  857.     buffer))
  858.  
  859. (defun backward-incarnations (inc)
  860.   "Moves back ARG incarnations of Mathematica, as recognized
  861. by math-header-re."
  862.   (if inc
  863.       (let ((count (cond ((numberp inc) inc)
  864.              ((equal inc '(4)) 1)
  865.              ((equal inc '(16)) 2)
  866.              ((equal inc '(64)) 3)
  867.              ((equal inc '(256)) 4)
  868.              ((equal inc '(1024)) 5)
  869.              (t (error "I'm too lazy to count that many prefix keys")))))
  870.         (re-search-backward math-header-re nil nil count))))
  871.  
  872. (defun math-copy-cell (numberstring incarnations pt)
  873.   "Copies the cell beginning In[<CELLNUMBER>] to the end of the buffer.  
  874. With CELLNUMBER of empty string and point at or after last In[...]:= 
  875. copies previous In cell to end of buffer.  With point before last In[...]:= 
  876. copies cell near point (In, Out, or just a block of text) to end of buffer.  
  877. With an explicit CELLNUMBER, a prefix arg will skip back prefix arg 
  878. incarnations before searching for In[<CELLNUMBER>].  C-u's count in unary.  
  879. When called from a program, CELLNUMBER must be a string, second arg is 
  880. INCARNATIONS back and third is POINT to begin search at."
  881.   (interactive "sCell number (default is cell near point):  \nP\nd")
  882.   (cond  ((zerop (length numberstring))
  883.       (goto-char (point-max))
  884.       (if (and
  885.            (re-search-backward "^In\\[[0-9]+\\]:=" nil t)
  886.            (>= pt (point)))
  887.           (progn
  888.         (re-search-backward "^In\\[[0-9]+\\]:=") ; back up to previous one
  889.         (while (and (not (bobp))
  890.                   (or (looking-at ; reject ones without any useful text 
  891.                    "^In\\[[0-9]+\\]:=\\s *\\(\\'\\|\n\\s *$\\)")))
  892.           (re-search-backward "^In\\[[0-9]+\\]:=")))
  893.         (goto-char pt))) ; do current cell
  894.      (t
  895.       (goto-char (point-max))
  896.       (backward-incarnations incarnations)
  897.       (re-search-backward (concat "^In\\[" numberstring "\\]:="))))
  898.   (if (interactive-p) (push-mark))
  899.   (let* ((cellinfo (math-identify-cell (point) nil t))
  900.      (copy (buffer-substring (car cellinfo) (nth 1 cellinfo))))
  901.     (if (not (equal (get-buffer math-process-buffer)
  902.             (current-buffer)))
  903.     (pop-to-buffer math-process-buffer))
  904.     (goto-char (point-max))
  905.     (re-search-backward "\\S ")
  906.     (forward-line 0)
  907.     (if (looking-at "^In\\[[0-9]+\\]:=\\s *$")
  908.     (end-of-line)
  909.       (goto-char (point-max)))
  910.     (insert copy)))
  911.  
  912. (defun math-isearch-backward ()
  913.   "Does a backward regexp i-search, initialized to find In[...]:="
  914.   (interactive)
  915.   (setq search-last-regexp "^In\\[[0-9]+\\]:=\\s *")
  916.   (setq unread-command-char search-reverse-char)
  917.   (isearch-backward-regexp))
  918.  
  919. (defun math-identify-cell (pt &optional procmark out-ok)
  920.   "Finds cell around POS. 
  921. Optional second arg PROCMARK (normally the process-mark) will bound the 
  922. search if the boundary otherwise would be a blank line (or the 
  923. beginning of the buffer) and POS >= PROCMARK. Optional 3rd arg OUT-OK
  924. will allow acceptance of Out cells as well as In cells. Returns a list of
  925. the buffer position of the beginning and end of the cell."
  926.   (save-excursion
  927.     (goto-char pt)
  928.     ;; back up at most one blank line looking for input
  929.     (end-of-line)
  930.     (re-search-backward  
  931.      (if out-ok
  932.      "\\(^In\\[[0-9]+\\]:= ?\\)\\|\\(^\\s *\n\\)\\|\\(^Out\\[[0-9]+\\]\\(//[^=]*\\)?= ?\\)"
  933.        "\\(^In\\[[0-9]+\\]:= ?\\)\\|\\(^\\s *\n\\)") nil 1)
  934.     (goto-char (cond ((match-end 1))
  935.              ((and out-ok (match-end 3)))
  936.              ((and procmark (>= pt procmark))
  937.               (max procmark 
  938.                (or (match-end 2) 
  939.                    (point-min) )))
  940.              ((match-end 2))
  941.              ((point-min))))
  942.     (let ((bpt (point))
  943.       ept
  944.       )
  945.       (if (re-search-forward "^\\s *$\\|^Out\\[[0-9]+\\][^=\n]*=\\|^In\\[[0-9]+\\]:=" nil 1)
  946.       ;; If it matches, we have found the beginning of a line
  947.       ;;  following the cell.  Back up one character.  
  948.       ;; If it doesn't match we are at eob and end of cell.
  949.       (goto-char (max (- (match-beginning 0) 1) bpt)))
  950.       (setq ept (point))
  951.       (list bpt ept))))
  952.  
  953.  
  954.  
  955. (defun get-math-completion (prefix)
  956.   "Returns string to insert to complete a Mathematica symbol
  957.   Designed to be called as in (insert (get-math-completion word))"
  958.   (let ((cbuf (current-buffer)))
  959.     (unwind-protect
  960.     (progn
  961.       (set-buffer " Mathwork")
  962.       (goto-char (point-min))
  963.       (let (alist)
  964.         (while (looking-at "\\S +")
  965.           (setq alist (cons (list (buffer-substring (match-beginning 0) (match-end 0))) alist))
  966.           (forward-line 1))
  967.         (set-buffer cbuf)
  968.         (let ((t-c-result  (and alist (try-completion prefix alist))))
  969.           ; try-completion barfs on a nil alist, so we help it out
  970.           (cond ((eq t-c-result t) 
  971.              (message "%s is complete" prefix)
  972.              "")
  973.             ((eq t-c-result nil)
  974.              (message "No match found")
  975.              "")
  976.             ((not (string= prefix t-c-result))
  977.              (substring t-c-result (length prefix)))
  978.             (t (with-output-to-temp-buffer "*Help*"
  979.              (display-completion-list 
  980.               (all-completions prefix alist))
  981.              (print-help-return-message))
  982.                "")))))
  983.       (set-buffer cbuf); unwind-protect exit
  984.    )))
  985.  
  986. (defun kill-9-process ()
  987.   "Kills the process in the current buffer as in kill -9."
  988.   (interactive)
  989.   (kill-process (get-buffer-process (current-buffer))))
  990.  
  991. (defun metered-process-send-string (process string)
  992.   "The same semantics as process-send-string, except the
  993. string is broken into small enough chunks to not mess up emacs."
  994.   (let ((p 0)
  995.     (len (length string)))
  996.     (while (< p len)
  997.       (process-send-string process
  998.                (substring string p (setq p (min len (+ p 80))))))))
  999.  
  1000.  
  1001.  
  1002. (defun skip-over-white-lines ()
  1003.   ;; it might be possible to do this with 
  1004.   ;; (re-search-forward "\\(^\\s *\n\\)*")
  1005.   ;; but this works.
  1006.   (while (and 
  1007.       (not (eobp))
  1008.       (looking-at "^\\s *$") ; blank line
  1009.       (zerop (forward-line)))))
  1010.  
  1011. ;;; Mathematica counts lines wrong.  The following is what really happens.
  1012. ;;; Blank lines, other than the first line are not counted (unless inside 
  1013. ;;; quotes or comments).  Unescaped newlines inside strings count double.
  1014.  
  1015. (defun goto-matherr-line (argline)
  1016.   "Goes to the line intended by Mathematica error messages"
  1017.   (interactive "ngoto Mathematica line number: ")
  1018.   (let ((lineno 1)
  1019.     (comment-depth 0)
  1020.     instring)
  1021.     (goto-char (point-min))
  1022.     (if (looking-at "^\s *$")
  1023.     (progn
  1024.       (setq lineno 2)
  1025.       (skip-over-white-lines)))
  1026.     (while (and (not (eobp))
  1027.         (< lineno argline))
  1028.       (cond ((> comment-depth 0)
  1029.          (re-search-forward "\\((\\*\\)\\|\\(\\*)\\)\\|\\(\n\\)" nil 1)
  1030.                     ; (* or *) or newline
  1031.          (cond ((match-beginning 1)
  1032.             (setq comment-depth (1+ comment-depth)))
  1033.            ((match-beginning 2)
  1034.             (setq comment-depth (1- comment-depth)))
  1035.            ((match-beginning 3)
  1036.             (setq lineno (1+ lineno)))))
  1037.         (instring
  1038.          (re-search-forward 
  1039. "\\(\\\\\\\\\\)\\|\\(\\\\\042\\)\\|\\(\042\\)\\|\\(\\\\\n\\)\\|\\(\n\\)" nil 1)
  1040.          ;; \\ or \quote or quote or \newline or newline
  1041.          ;;  \042 is a double quote.  
  1042.          ;; Using the octal form keeps Emacs from getting lost.
  1043.          (cond ((match-beginning 3)
  1044.             (setq instring nil))
  1045.            ((match-beginning 4)
  1046.             (setq lineno (1+ lineno)))
  1047.            ((match-beginning 5)
  1048.             (setq lineno (+ 2 lineno)))))
  1049.         (t
  1050.          (re-search-forward "\\(\050\\*\\)\\|\\(\042\\)\\|\\(\n\\)" nil 1)
  1051.                     ; left paren* or quote or newline
  1052.          (cond ((match-beginning 1)
  1053.             (setq comment-depth 1))
  1054.            ((match-beginning 2)
  1055.             (setq instring t))
  1056.            ((match-beginning 3)
  1057.             (setq lineno (1+ lineno))
  1058.             (skip-over-white-lines))))))))
  1059.  
  1060. (defun find-math-error ()
  1061.   "Searches for the last \"syntax error in\" message; goes to indicated line
  1062. in the indicated file.  It uses the symbol Mathematica-search-path rather 
  1063. than going to all the work to discover the real real search path."
  1064.   (interactive)
  1065.   (let (filename
  1066.     linenumber
  1067.     raw-filename
  1068.     (math-search-path Mathematica-search-path))
  1069.     (save-excursion
  1070.       (re-search-backward "syntax error in")
  1071.       (forward-line 0)
  1072.       (if (not (looking-at "\\([^:]+\\): *\\([0-9]+\\):"))
  1073.       (error "Cannot parse error line"))
  1074.       (setq raw-filename (buffer-substring (match-beginning 1) (match-end 1)))
  1075.       (setq linenumber (string-to-int 
  1076.             (buffer-substring (match-beginning 2) (match-end 2)))))
  1077.     (while (not filename)
  1078.       (setq filename (expand-file-name raw-filename (car math-search-path)))
  1079.       (if (not (file-readable-p filename))
  1080.       (progn (setq filename nil)
  1081.          (setq math-search-path (cdr math-search-path))
  1082.          (if (null math-search-path)
  1083.              (error "File %s not found" raw-filename)))))
  1084.     (find-file-other-window filename)
  1085.     (goto-matherr-line linenumber)))
  1086.  
  1087.  
  1088. (defun set-math-process-buffer (buffer)
  1089.   "Sets the buffer in/to which to evaluate/copy Mathematica
  1090. code.  (You only need to use this function if you want a buffer 
  1091. other than *math*.)"
  1092.   (interactive "bMathematica buffer: ")
  1093.   (make-local-variable 'math-process-buffer)
  1094.   ;; The following trick will use the buffer itself if
  1095.   ;; it is defined.  That way if the user eventually 
  1096.   ;; changes the name, say by writing it out, this local
  1097.   ;; math-process-buffer will still point to the right place.  
  1098.   ;; But if the buffer does not yet exist, it will still work.
  1099.   (setq math-process-buffer (or (get-buffer buffer) buffer)))
  1100.